<?php

# Display blog. See blog.php.

# TODO: when enabling namespace use G::Label instead of Label and so on...
#use Giocondo as G;

class Blog {
	static public function display() {
		# TODO: use the registry pattern instead of global variables (autoload the registry class)
		global $Blog_list, $Blog_box;

		# Create blog database table if not already exists.
		# Date and time are stored in standard ISO 8601 format.
		# They are human readable and lexicographic order is the same as date order.
		$db = Db::singleton();
		$db->exec('CREATE TABLE IF NOT EXISTS blog(id INTEGER PRIMARY KEY AUTOINCREMENT,
			date CHAR(25), title CHAR(100), text BLOB)');

		# Fetch and display all posts.
		$Blog_list = array();
		# Display entries in reverse-chronological order.
		if ($result = $db->query('SELECT * FROM blog ORDER BY id DESC')) {
			foreach (array('id', 'date', 'title', 'text') as $field) {
				$result->bindColumn($field, $$field);
			}

			while ($result->fetch(PDO::FETCH_BOUND)) {
				if (count($Blog_list)) {
					$Blog_list[] = new Separator();
				}
				$Blog_list[] = Format::post($date, $title, $text);
			}
		}

		$Blog_list[] = new Button(array(
			'text'      => 'New',
			'onClicked' => 'NewWin::display()'
		));

		$Blog_box = new Box(array(
			'label'       => 'My Blog',
			'orientation' => 'vertical',
			'children'    => $Blog_list
		));

		$scroll_win = new ScrolledWindow($Blog_box);
		$main_win = new Window(array(
			'title' => 'Giocondo example: blog',
			'child' => $scroll_win
		));

		main_loop();
		# There should be no more code after main_loop.
	}
}

# End of file
# vim: set ts=2 sw=2 noet:
?>
